package app.actions;

import java.awt.event.ActionEvent;
import java.util.Collection;
import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
import javax.swing.JTree;
import libs.Attendance;
import libs.Seminar;
import libs.Subject;

/**
*          This action removes all seminars of the subject.
 *
 * @author papi
 */
public class RemoveAllSeminarsAction extends AbstractAction {

    private Attendance attendance;

    private JTree tree;

    public RemoveAllSeminarsAction(Attendance attendance) {
        if (attendance == null) {
            throw new NullPointerException("attendance");
        }
        this.attendance = attendance;
    }

    public void setTree(JTree tree) {
        if (tree == null) {
            throw new NullPointerException("tree");
        }
        this.tree = tree;
    }

    @Override
    public void actionPerformed(ActionEvent evt) {
        if(tree == null)
            throw new NullPointerException("tree is still null!");
        Subject subject = (Subject)tree.getSelectionPath().getPathComponent(1);
        String title = java.util.ResourceBundle.getBundle("app/frames/language").getString("QuestionMSG");
        String message = java.util.ResourceBundle.getBundle("app/frames/language").getString("ReallyRemoveAllSeminarsMSG")
                + " " + subject;
        if(JOptionPane.showConfirmDialog(null,message,title,
            JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == 0)
        {
            Collection<Seminar> seminars = attendance.getSeminars(subject);
            for (Seminar seminar : seminars) {
                attendance.removeSeminar(seminar);
            }
            tree.getModel().valueForPathChanged(tree.getSelectionPath().getParentPath(), null);
        }
    }
}
